package com.palfund.glide.util;

import com.bumptech.glide.load.model.GlideUrl;

/**
 * Created by clvc on 2017/8/29.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 * <p/>
 * 重新设置Glide缓存的key,避免url地址为动态变化的
 */

public class OptGlideUrl extends GlideUrl {
    private String mUrl;

    public OptGlideUrl(String url) {
        super(url);
        mUrl = url;
    }

    @Override
    public String getCacheKey() {

        return mUrl.replace(findTokenParam(), "");
    }

    private String findTokenParam() {
        String tokenParam = "";
        int tokenKeyIndex = mUrl.indexOf("?token=") >= 0 ? mUrl.indexOf("?token=") : mUrl.indexOf
                ("&token=");
        if (tokenKeyIndex != -1) {
            int nextAndIndex = mUrl.indexOf("&", tokenKeyIndex + 1);
            if (nextAndIndex != -1) {
                tokenParam = mUrl.substring(tokenKeyIndex + 1, nextAndIndex + 1);
            } else {
                tokenParam = mUrl.substring(tokenKeyIndex);
            }
        }
        return tokenParam;
    }

}
